<?php
/**
 * Description of FKReferenceStringManager
 *
 * @author Magno
 */
class FKReferenceStringManager {
    
    private $tableString;
    private $tableMeta;
    private $tablesMeta;

    function __construct($tableString, $tableMeta, $tablesMeta) {
        $this->tableString = $tableString;
        $this->tableMeta = $tableMeta;
        $this->tablesMeta = $tablesMeta;
    }

    public function generateAllFKMeta() {
        $stringTemp = $this->tableString;

        $stringAux = str_replace(" ", "", $stringTemp);
        $stringAux = str_replace("\n", "", $stringAux);
        $stringAux = str_replace("\r", "", $stringAux);

        $fksReference = array();

        while(stripos($stringAux, Constantes::$CONSTRAINT)){
            $indIni = stripos($stringAux, Constantes::$CONSTRAINT);
            $stringAux = substr($stringAux, $indIni);
            
            $indIni = 0;
            $indFinal = 0;
            
            for ($i = 0; $i < strlen($stringAux); $i++) {
                if(strcmp($stringAux[$i], "(") == 0){
                    do{
                        $i++;
                    }while(strcmp($stringAux[$i], ")") != 0);
                    $i++;
                } 
                if(strcmp($stringAux[$i], ",") == 0 || $i == (strlen($stringAux)-1)){
                    $indFinal = $i;
                    break;
                }

            }
                        
            $constraintString = trim(substr($stringAux, $indIni, $indFinal+1));
            $fk = $this->generateFKMeta($constraintString);
            $fk->getTableReference()->addFKReferenciada($fk);
            $fksReference[$fk->getName()] = $fk;
            $stringAux = trim(substr($stringAux, $indFinal));

        }

        return $fksReference;
    }

    public function generateFKMeta($constraintString) {

        $tableFK = $this->tableMeta;
        $name = $this->getFKName($constraintString);
        $attributes = ($this->getFKAttributes($constraintString));
        $tableReference = $this->getFKTableReference($constraintString);

        if($tableReference == null)
            throw new Exception ("Erro na composição da FK, tabela referenciada não encontrada!!!");

        $references = $this->getFKAttributesReference($constraintString, $tableReference);

        return new FKReferenceMeta($name, $attributes, $tableFK, $tableReference, $references, 
                Constantes::$CARDINALITY_1, Constantes::$SIDE_OBJ_FK, false, Constantes::$TYPE_FORM_NO);

    }

    public function getFKName(&$constraintString) {

        $indIni = stripos($constraintString, Constantes::$CONSTRAINT)+strlen(Constantes::$CONSTRAINT);
        $constraintString = substr($constraintString, $indIni);
        $indFim = stripos($constraintString,  Constantes::$FOREIGNKEY);

        return substr($constraintString, 0,$indFim);
    }

    public function getFKTableReference(&$constraintString) {

        $indIni = stripos($constraintString, Constantes::$REFERENCES)+strlen(Constantes::$REFERENCES);

        if(stripos($constraintString, ".")){
            $indIni = stripos($constraintString, ".")+1;
        }

        $constraintString = substr($constraintString, $indIni);
        $indFim = stripos($constraintString, "(");

        $name = substr($constraintString, 0,$indFim);

        return $this->tablesMeta[$name];

    }

    public function getFKAttributes(&$constraintString) {

        $indIni = stripos($constraintString, Constantes::$FOREIGNKEY."(")+strlen(Constantes::$FOREIGNKEY."(");
        $constraintString = substr($constraintString, $indIni);
        $indFim = stripos($constraintString, ")");

        $attrs = explode(",",substr($constraintString, 0,$indFim));
        $attrsMeta = array();

        for ($m = 0; $m < count($attrs); $m++) {
            $atM = $this->tableMeta->getAttributes();
            $attrsMeta[] = $atM[$attrs[$m]];
        }

        return $attrsMeta;

    }

    public function getFKAttributesReference(&$constraintString, $tableReference) {
     
        $indIni = stripos($constraintString, "(")+1;
        $constraintString = substr($constraintString, $indIni);
        $indFim = stripos($constraintString, ")");

        $attrs = explode(",",substr($constraintString, 0,$indFim));

        $attrsMeta = array();

        for ($m = 0; $m < count($attrs); $m++) {
            $atM = $tableReference->getAttributes();
            $attrsMeta[] = $atM[$attrs[$m]];
        }

        return $attrsMeta;

    }

}
?>
